package rpg.core
{
	import rpg.Utils.GameUtils;
	import rpg.role.SceneObject;

	public class FlyAnimationComponent extends AnimationComponent
	{
		private var mapX : int;
		private var mapY : int;

		private var targetX : int;
		private var targetY : int;

		private var targetSceneObj : SceneObject;
		public var needRotate : Boolean;
		public var speed : Number;

		public function FlyAnimationComponent(type : * = null)
		{
			super(type);
		}

		override public function update(delay : uint) : void
		{
			if(animation == null)
				return;
			runTimes += delay;
			if(runTimes > animationDelay)
			{
				animation.tryNext(delay);
				var moveDst : Number = speed * delay;
				var dx : int = GameUtils.cosD(rotation) * moveDst;
				var dy : int = GameUtils.sinD(rotation) * moveDst;
				//				mapX += dx;
				//				mapY += dy;
				var tmpTargetX : int = targetSceneObj ? targetX : targetSceneObj.mapX;
				var tmpTargetY : int = targetSceneObj ? targetY : (targetSceneObj.mapY - targetSceneObj.height * .5);
				var dst : int = GameUtils.getDistance(mapX , mapY , tmpTargetX , tmpTargetY);
				if(needRotate && animation.curFrame)
				{
					//rotation = GameUtils.getRotation(mapX , mapY , tmpTargetX , tmpTargetY);
				}
				if(dst < moveDst || !targetSceneObj.isActive)
				{
					destroy();
					return;
				}
				updateRender(mapX - GameUtils.viewX , mapY - GameUtils.viewY , offsetX , offsetY);
			}
		}

		public function setStartPosition(mapX : int , mapY : int) : void
		{
			this.mapX = mapX;
			this.mapY = mapY;
		}

		public function setTargetPosition(targetX : int , targetY : int) : void
		{
			this.targetX = targetX;
			this.targetY = targetY;
			targetSceneObj = null;
		}

		public function setTarget(target : SceneObject) : void
		{
			targetX = -1
			targetY = -1
			targetSceneObj = target;
		}

		override public function destroy() : void
		{
			super.destroy();
			targetSceneObj = null;
		}
	}
}